package ledrpt

import org.kdx.DateUtils

import java.text.SimpleDateFormat

class MachineController {

    def dynamicQueryService

    def Machine() {
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 10
        SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd ')
        def startTime = params.startTime ? sdf.parse(params.startTime) : sdf.parse(sdf.format(new Date().clearTime() - 30))
        def endTime = params.endTime ? sdf.parse(params.endTime) : sdf.parse(sdf.format(new Date()))
        params.startTime = sdf.format(startTime)
        params.endTime = sdf.format(endTime)
        params.startNo = (pageSize * (pageCurrent - 1)) + 1
        params.endNo = pageSize * pageCurrent
        def dayList = DateUtils.getHourList(startTime, endTime)

        params.SYS_QUERY_NAME = 'package10'
        def package10 = dynamicQueryService.queryForList(params)

        params.SYS_QUERY_NAME = 'machine'
        def machine = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'machineTotal'
        def machineTotal = dynamicQueryService.queryForList(params)
        def legendDataList = ['QTY', 'YIELD',]
        def percentItemList = ['YIELD']
        def seriesData = [[85, 90], [85, 90]]
        def pageSizeList = [10, 20, 50, 100]
        params.SYS_QUERY_NAME='machineCount'
        def machineCount =dynamicQueryService.queryForList(params)
        def totalCount=machineCount.tatolCount[0]




        [startTime: startTime,part_name:params.part_name, endTime: endTime, totalCount:totalCount,pageSizeList: pageSizeList, pageCurrent: pageCurrent, pageSize: pageSize,
         dayList  : dayList, machine: machine,package10:package10,RESERVED1:params.RESERVED1,machineTotal:machineTotal,legendDataList: legendDataList, percentItemList: percentItemList, seriesData: seriesData
        ]
    }

    def exportExcel() {
        def sysQueryName
        sysQueryName = params.SYS_QUERY_NAME

        params.startNo = 1
        params.endNo = 99999999999999999
        params.startno = 1
        params.endno = 99999999999999999
        params.SYS_QUERY_NAME = sysQueryName

        def dataList = dynamicQueryService.queryForList(params)

        def tableHeadList = []
        if (dataList.size() > 0) {
            tableHeadList = dataList[0].keySet()
        }


        params.SYS_QUERY_NAME = 'machine'
        def machine = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'machineTotal'
        def machineTotal = dynamicQueryService.queryForList(params)

        String filename = new String("${sysQueryName}.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        render(template: "export${params.EXPORT_CONTENT_NAME}",
                model: [dataList: dataList, tableHeadList: tableHeadList,
                        machine:machine,
                        machineTotal:machineTotal
                ])
    }
}